<?php
/**
 * Created by PhpStorm.
 * User: youZi
 * Date: 2017/7/13
 * Time: 10:05
 */

namespace Task\App\Restaurantprintstat;

use Task\App\Base\BuildBaseInfo;
use Task\Core\Base;
use Task\Core\RedisQueueService;
use Task\Core\RedisService;

class RestaurantPrintTerminal extends Base
{
    public $mysql;

    public function __construct()
    {
        parent::__construct();
        $this->mysql = $this->selectDB('mysql');
    }

    /****************************************************数据统计******************************************************/
    /*
     * 数据统计
     */
    public function dataCount()
    {
        wLog('enter','restaurantprint');
        $start = date('Y-m-d',time()-86400);
        $end = date('Y-m-d');
        $sql = "SELECT `sid`,date(create_time) tj_date,terminal_code device_id,`terminal_name`,website_name address,SUM(order_amount) money,count(order_id) num,`pay_type` FROM `gc_restaurant_print_order` WHERE (  (`create_time` BETWEEN '$start' AND '$end' ) ) AND ( `status` = 1 ) GROUP BY sid,terminal_code,pay_type ORDER BY tj_date asc,terminal_code asc";
        $list = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
        if(!$list){
            wLog('$res'. print_r($this->mysql->debug()),'restaurantprint');
        }
        $sql2 = "SELECT `sid`,`account_id` FROM `gc_restaurant_print_set` WHERE ( `cut_off` = 0 )";
        $accountId = $this->mysql->query($sql2)->fetch(\PDO::FETCH_ASSOC);
        if(!$accountId){
            wLog('$res'. print_r($this->mysql->debug()),'restaurantprint');
        }
        $data = array();
        foreach ($list as &$v) {
            $v['account_id'] = $accountId['sid'];
            $v['create_time'] = date('Y-m-d H:i:s');
            $v['update_date'] = date('Y-m-d H:i:s');
            $v['type_id'] = 99;
            unset($v['sid']);
            $data = array(
                'account_id' => $v['account_id'],
                'type_id' => $v['type_id'],
                'tj_date' => $v['tj_date'],
                'device_id' => $v['device_id'],
                'pay_type' => $v['pay_type']
            );
            $res = $this->mysql->get('restaurant_print_financial','*',array('AND'=>$data));
            if($res){
                $res = $this->mysql->update('restaurant_print_financial',$v,array('id'=>$res['id']));
            }else{
                $res = $this->mysql->insert('restaurant_print_financial',$v);
            }
            if(!$res){
                wLog('$res'. print_r($this->mysql->debug()),'restaurantprint');
            }
        }
        wLog('end','restaurantprint');
    }
}